<template>
  <div>
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="drawer = true">新增</el-button>
      </el-col>
      <el-col :span="12">
        <el-input placeholder="请输入内容" prefix-icon="el-icon-search" v-model="input"></el-input>
      </el-col>
    </el-row>
    <!--  -->
    <el-table :data="tableData" stripe style="width: 100%;margin-top:15px">
      <el-table-column type="index" label="#" width="180"></el-table-column>
      <el-table-column prop="title" label="题目" width="400"></el-table-column>
      <el-table-column prop="cid" label="分类" width="180"></el-table-column>
      <el-table-column prop="uid" label="作者"></el-table-column>
      <el-table-column prop="created" label="发布时间"></el-table-column>
      <el-table-column width="300" label="操作">
        <template slot-scope="scope">
          <el-button size="mini">编辑</el-button>
          <el-button size="mini" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--  -->
    <el-pagination small layout="prev, pager, next" :total="50"></el-pagination>
    <!--  -->
    <el-drawer title="我是标题" size="60%" :visible.sync="drawer" :with-header="false">
      <div style="margin:30px">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="题目">
            <el-input type="textarea" autosize :show-word-limit="true" v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="分类">
            <el-select v-model="form.cid" placeholder="请选择类别">
              <el-option
                v-for="(item,index) in classify"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选项">
            <div class="dis" v-for="(item,index) in form.options" :key="index">
              <el-checkbox :label="item.text" v-model="form.answers"></el-checkbox>
              <el-input
                type="textarea"
                :show-word-limit="true"
                autosize
                v-model="item.value"
                style="width:80%"
              ></el-input>
              <template>
                <el-button type="success" icon="el-icon-minus" @click="reduceChoice" circle></el-button>
                <el-button
                  v-if="index+1 == form.options.length"
                  type="primary"
                  icon="el-icon-plus"
                  @click="addChoice"
                  circle
                ></el-button>
              </template>
            </div>
          </el-form-item>
          <el-form-item label="答案">
            <div>{{doSort(form.answers).join(',') || ''}}</div>
          </el-form-item>
          <el-form-item label="答题提示">
            <el-input type="textarea" autosize :show-word-limit="true" v-model="form.tips"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">保存</el-button>
            <el-button type="success">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  layout: "mine",
  data() {
    return {
      input: "",
      drawer: false,
      form: {
        answers: [],
        options: [
          { text: "A", value: "" },
          { text: "B", value: "" },
          { text: "C", value: "" },
          { text: "D", value: "" }
        ],
        uid: "1"
      },
      initOptions: [
        { text: "A", value: "" },
        { text: "B", value: "" },
        { text: "C", value: "" },
        { text: "D", value: "" }
      ]
    };
  },
  async fetch({ $axios, store }) {
    await store.dispatch("classify/getData", { pageSize: 70 });
    store.dispatch("questions/getData");
  },
  methods: {
    ...mapActions("questions", ["getData", "addData"]),
    async onSubmit() {
      this.form.answers = this.doSort(this.form.answers);
      await this.addData(this.form);
      this.getData();
      console.log(this.form);
    },
    // 增加选择
    addChoice() {
      this.form.options.push({ text: "E", value: "" });
    },
    // 减少选择
    reduceChoice() {
      if (this.form.options.length <= 4) {
        this.$message({
          message: "至少有四个选项，不能再删了!!!",
          type: "warning"
        });
        return;
      }
      this.form.options.pop();
    },
    doSort(val) {
      let app = [...val];
      app.sort();
      return app;
    }
  },
  computed: {
    ...mapState("questions", ["data"]),
    classify() {
      return this.$store.state.classify.data;
    },
    tableData() {
      let classtableData = JSON.parse(JSON.stringify(this.data));
      classtableData.map(item => {
        this.classify.forEach(element => {
          item.cid == element.id ? (item.cid = element.name) : "";
        });
      });
      return classtableData;
    }
  }
};
</script>

<style scoped>
</style>